﻿using AutoMapper;
using ImoviesBlazor.Server.Helpers;
using ImoviesBlazor.Server.Repository;
using ImoviesBlazor.Shared.Dtos;
using ImoviesBlazor.Shared.Entities;
using ImoviesBlazor.Shared.Utils;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace ImoviesBlazor.Server.Controllers
{
    /// <summary>
    /// 年度点评控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    public class AnnualReviewController : ControllerBase
    {
        private readonly IAnnualReviewRepository _annualReviewsRepository;
        private readonly IMapper _mapper;

        public AnnualReviewController(IAnnualReviewRepository annualReviewsRepository, IMapper mapper)
        {
            this._annualReviewsRepository = annualReviewsRepository;
            this._mapper = mapper;
        }

        /// <summary>
        /// 获取年度点评
        /// </summary>
        /// <param name="vcate">视频种类枚举AnnualReviewsVideoCate</param>
        /// <returns></returns>
        [HttpGet("{vcate}")]
        public async Task<ActionResult<IEnumerable<AnnualReviewDto>>> GetList([FromRoute] AnnualReviewsVideoCate vcate)
        {
            // 先从缓存中读取
            var cacheData = CacheUtils.GetData<List<AnnualReviewDto>>(CacheKeyConst.ANNUAL_REVIEW_LIST);
            if (cacheData != null)
            {
                return Ok(cacheData);
            }

            var ars = await _annualReviewsRepository.GetListAsync(vcate);
            var data = _mapper.Map<IEnumerable<AnnualReviewDto>>(ars);

            // 设置缓存
            CacheUtils.SetData<List<AnnualReviewDto>>(CacheKeyConst.ANNUAL_REVIEW_LIST, data.ToList());

            return Ok(data);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="movie"></param>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        public async Task<ActionResult> AddAnnualReview([FromBody] AnnualReviewForCreateDto annualReview)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest();
            }
            var a = _mapper.Map<AnnualReview>(annualReview);
            _annualReviewsRepository.AddAnnualReview(a);
            bool res = await _annualReviewsRepository.SaveAsync();
            if (!res)   //保存失败
            {
                Response.StatusCode = 500;
                return Content("操作失败");
            }
            return Ok();
        }


        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="movie"></param>
        /// <returns></returns>
        [Authorize]
        [HttpPut]
        public async Task<ActionResult> UpdateAnnualReview([FromBody] AnnualReviewForUpdateDto annualReview)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest();
            }
            var a = _mapper.Map<AnnualReview>(annualReview);
            _annualReviewsRepository.UpdateAnnualReviewAsync(a);
            bool res = await _annualReviewsRepository.SaveAsync();
            if (!res)   //保存失败
            {
                Response.StatusCode = 500;
                return Content("操作失败");
            }
            return Ok();
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Authorize]
        [HttpDelete("{id}")]
        public async Task<ActionResult<bool>> DeleteAnnualReview([FromRoute] uint id)
        {
            await _annualReviewsRepository.DeleteAnnualReviewAsync(id);
            bool res = await _annualReviewsRepository.SaveAsync();
            if (!res)   //保存失败
            {
                Response.StatusCode = 500;
                return Content("操作失败");
            }
            return Ok();
        }
    }
}
